//
//  HomeCanTestViewController.m
//  ZJControlsClass
//
//  Created by mac on 2024/11/15.
//

#import "HomeCanTestViewController.h"
#import "HomeCanTestTableViewCell.h"
#import "HomeAvailableNetworking.h"
#import "HomeAvailableModel.h"
#import "publicNodataView.h"



@interface HomeCanTestViewController ()<UITableViewDelegate,UITableViewDataSource,protocolReloadTableViewDelegate>

@property (nonatomic,strong) UIView * alertView;
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * dataSource;
@property (nonatomic,strong) HomeAvailableNetworking * manager;

@property (nonatomic,strong) publicNodataView * nodataView;


@end

@implementation HomeCanTestViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = COLOR_RGB(241, 241, 241);
    
    
    [self setUpUI];
    
    
}


-(void)setUpUI{
    
//    [self.view addSubview:self.alertView];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.nodataView];
//    [self.alertView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.top.right.equalTo(self.view);
//        make.height.equalTo(@(ALIGN_HEIGHT_PT(45)));
//    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.top.equalTo(self.view);
//        make.top.equalTo(self.alertView.mas_bottom);
    }];
    
    [self.nodataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.view);
    }];
    
    
    
    [self loadNet];
    
    
    
}


-(void)loadNet{
    
    [self.manager dispatchTaskWithParam:@{} completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (responseObject && [responseObject[@"code"] intValue] == 200){
            [self.dataSource removeAllObjects];
            [self.dataSource addObjectsFromArray:[HomeAvailableModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]]];
            
            if (self.dataSource.count == 0) {
                self.nodataView.hidden = NO;
            }else{
                self.nodataView.hidden = YES;
            }
            
            [self.tableView reloadData];
        }else{
            [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:^{
                
            }];
        }
    }];
    
    
    
}

-(void)reloadTableView{
    [self loadNet];
}



#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeCanTestTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeCanTestTableViewCell"];
    if (!cell){
        cell = [[HomeCanTestTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"HomeCanTestTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataSource[indexPath.row];
    cell.delegate = self;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ALIGN_HEIGHT_PT(250);
}




#pragma mark -- 懒加载
-(UIView *)alertView{
    if (!_alertView){
        _alertView = [UIView new];
        _alertView.backgroundColor = [UIColor whiteColor];
    }
    return _alertView;
}

-(UITableView *)tableView{
    if (!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
    }
    return _tableView;
}
-(NSMutableArray *)dataSource{
    if(!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

-(HomeAvailableNetworking *)manager{
    if (!_manager){
        _manager = [HomeAvailableNetworking new];
    }
    return _manager;
}

-(publicNodataView *)nodataView{
    if (!_nodataView) {
        _nodataView = [publicNodataView new];
        _nodataView.hidden = YES;
    }
    return _nodataView;
}


@end
